package com.berchen.数据结构.栈;

/**
 * 自定义栈
 */
public class ArrayStack {

    private int size;   // 栈大小
    private int[] stack;    // 栈
    private int top=-1; //表示栈顶  初始化-1

    public ArrayStack(int size) {
        this.size = size;
        stack=new int[this.size];
    }

    public int getSize(){
        return this.size;
    }

    public void list(){
        if(isEmpty()) throw new RuntimeException("栈空");
        for (int i =top;i>=0;i--){
            System.out.printf("%d\t" ,stack[i]);
        }
        System.out.println();
    }

    /**
     * 返回栈顶元素  并不出栈
     * @return
     */
    public int peek(){
        if(isEmpty()) throw new RuntimeException("栈空");
        return stack[top];
    }

    /**
     * 出栈
     * @return
     */
    public int pop(){
        if(isEmpty()) throw new RuntimeException("栈空");
        return stack[top--];
    }

    /**
     * 入栈
     * @param num
     */
    public void push(int num){
        if(isFull()) throw new RuntimeException("栈满");
        stack[++top]=num;
    }

    /**
     * 是否栈空
     * @return
     */
    public boolean isEmpty(){
        return top==-1;
    }

    /**
     * 是否栈满
     * @return
     */
    public boolean isFull(){
        return top==size-1;
    }
}
